cat('Creating Table A29 \n\n')

dat = read.csv('TableA29Figure7_4_data.csv')
dat.uo = dat[dat$uo.non.uo == 'uo',]
dat.sec = dat[dat$uo.non.uo != 'uo',]

##formulas specific to trust
use.formula = as.formula(paste('in.out.diff.dictator ~ ',master.formula,sep = ''))
use.formula.reduced = as.formula(paste('in.out.diff.dictator ~ ',master.formula.reduced,sep = ''))
use.formula.interactions = as.formula(paste('in.out.diff.dictator ~ ',master.formula.interactions,sep = ''))

##UO
##regressions
reg.uo.pub = lm(use.formula, data = dat.uo)
clustered.se = cl(dat.uo, reg.uo.pub, dat.uo$index.city)
reg.uo.pub$se = clustered.se

reg.uo.pub.reduced = lm(use.formula.reduced, data = dat.uo)
clustered.se = cl(dat.uo, reg.uo.pub.reduced, dat.uo$index.city)
reg.uo.pub.reduced$se = clustered.se

reg.uo.pub.interactions = lm(use.formula.interactions, data = dat.uo)
clustered.se = cl(dat.uo, reg.uo.pub.interactions, dat.uo$index.city)
reg.uo.pub.interactions$se = clustered.se

##Secular
##regressions
reg.sec.pub = lm(use.formula, data = dat.sec)
clustered.se = cl(dat.sec, reg.sec.pub, dat.sec$index.city)
reg.sec.pub$se = clustered.se

reg.sec.pub.reduced = lm(use.formula.reduced, data = dat.sec)
clustered.se = cl(dat.sec, reg.sec.pub.reduced, dat.sec$index.city)
reg.sec.pub.reduced$se = clustered.se

reg.sec.pub.interactions = lm(use.formula.interactions, data = dat.sec)
clustered.se = cl(dat.sec, reg.sec.pub.interactions, dat.sec$index.city)
reg.sec.pub.interactions$se = clustered.se

outtable = apsrtable(reg.uo.pub.reduced,
                     reg.uo.pub,
                     reg.uo.pub.interactions,
                     reg.sec.pub.reduced,
                     reg.sec.pub, 
                     reg.sec.pub.interactions,
                     Sweave = T,
                     stars = 'default',
                     coef.names = coef.names,
                     model.names = c('UO','UO','UO','Non UO','Non UO','Non UO'),
                     notes = ''
                      )
writeLines(
  outtable, 'TableA29.tex')


#####################################
###zelig for effects of segregation
cat('Creating Figure 7.4 \n\n')

##values for homogeneity and segregation 
mean.seg.value = .65
uo.homo.value = .73 
sec.homo.value = .27   

dissim.scale = seq(from = .288, to = .63, by = .01) 
homo.scale.uo = seq(from = .67, to = .99, by = .01)
homo.scale.sec = seq(from = .01, to = .33, by = .01)


##simulate for different levels of segregation
##UO
reg.uo = zelig(use.formula, data = dat.uo,
               model = 'ls',
               cite = F)

outputs.uo = matrix(nrow = length(dissim.scale), ncol = 3)
for(i in dissim.scale){
  row.i = which(dissim.scale == i)
  outx = setx(reg.uo, outgroup.dissim.yeshiva = i,  outgroup.yeshiva = uo.homo.value,
              jerusalem = 0,  	
              demo1.sex = 'male', 
              demo1.age = 40, 
              demo1.ethnicity = 'ash',
              demo2.left_right = 5.2,
              income = 'low', 
              high.school = 1, 
              nonjewish_pcnt = .06,
              immigrant = 0
            )
  
  sims = sim(reg.uo,outx, sims = n.sims)
  outs = sims$getqi(qi="ev")
  outputs.uo[row.i,1] = mean(outs)
  outputs.uo[row.i,2] = quantile(outs,.05)
  outputs.uo[row.i,3] = quantile(outs,.95)
  }


###secular
reg.sec = zelig(use.formula, data = dat.sec,
                model = 'ls',
                cite = F)

outputs.sec = matrix(nrow = length(dissim.scale), ncol = 3)
for(i in dissim.scale){
  row.i = which(dissim.scale == i)
  outx = setx(reg.sec, outgroup.dissim.yeshiva = i, outgroup.yeshiva = sec.homo.value,
              jerusalem = 0,  	
              demo1.sex = 'male', 
              demo1.age = 40, 
              demo1.ethnicity = 'ash',
              demo2.left_right = 5.2,
              income = 'low', 
              high.school = 1, 
              nonjewish_pcnt = .06,
              immigrant = 0
               )
  
  sims = sim(reg.sec,outx, sims = n.sims)
  outs = sims$getqi(qi="ev")
  outputs.sec[row.i,1] = mean(outs)
  outputs.sec[row.i,2] = quantile(outs,.05)
  outputs.sec[row.i,3] = quantile(outs,.95)
    }



##segregation plots
uo.frame = as.data.frame(cbind(outputs.uo,dissim.scale))
out.plot.uo.seg = ggplot(uo.frame, aes(dissim.scale,V1)) + 
  geom_smooth(color = 'black', aes(alpha = .75), se = FALSE) +
  geom_ribbon(aes(ymin = V2, ymax = V3), alpha = .05) +
  theme(legend.position="none") +
  labs(y = 'Bias in Sharing') + 
  scale_x_continuous(breaks = c(min(dissim.scale),max(dissim.scale)),
                     labels = c('Tel Aviv','Jerusalem')) +
  theme(
    plot.background = element_blank()
    ,panel.grid.minor = element_blank()
    ,panel.border = element_blank(),
    axis.text=element_text(size=8),
    axis.title=element_text(size=12.5),
    plot.margin = unit(c(5,15,5,5), "pt"),
    axis.title.y=element_blank(),
    axis.text.y=element_text(color = 'white'),
    axis.ticks.x=element_blank(),
    axis.title.x=element_blank(),
    axis.text.x=element_text(hjust=c(0,1), vjust = 0, face = 'italic')
  ) + 
  coord_cartesian(ylim=c(0,10))


sec.frame = as.data.frame(cbind(outputs.sec,dissim.scale))
out.plot.sec.seg = ggplot(sec.frame, aes(dissim.scale,V1)) + 
  geom_smooth(color = 'black', aes(alpha = .75), se = FALSE) +
  geom_ribbon(aes(ymin = V2, ymax = V3), alpha = .05) +
  theme(legend.position="none") +
  labs(y = 'Bias in Sharing') + 
  scale_x_continuous(breaks = c(min(dissim.scale),max(dissim.scale)),
                     labels = c('Tel Aviv','Jerusalem')) +
  theme(
    plot.background = element_blank()
    #,panel.grid.major = element_blank()
    ,panel.grid.minor = element_blank()
    ,panel.border = element_blank(),
    axis.text=element_text(size=8),
    axis.title=element_text(size=12),
    plot.margin = unit(c(5,15,5,5), "pt"),
    axis.title.y=element_blank(),
    axis.ticks.x=element_blank(),
    axis.title.x=element_blank(),
    axis.text.x=element_text(hjust=c(0,1), face = 'italic')
  ) + 
  coord_cartesian(ylim=c(0,10))



##simulate for different levels of homogeneity
##UO
outputs.uo = matrix(nrow = length(homo.scale.uo), ncol = 3)
for(i in homo.scale.uo){
  row.i = which(homo.scale.uo == i)
  outx = setx(reg.uo,  outgroup.yeshiva = i, outgroup.dissim.yeshiva = mean.seg.value,
              jerusalem = 0,  	
              demo1.sex = 'male', 
              demo1.age = 40, 
              demo1.ethnicity = 'ash',
              demo2.left_right = 5.2,
              income = 'low', 
              high.school = 1, 
              nonjewish_pcnt = .06,
              immigrant = 0
               )
  
  sims = sim(reg.uo,outx, sims = n.sims)
  outs = sims$getqi(qi="ev")
  outputs.uo[row.i,1] = mean(outs)
  outputs.uo[row.i,2] = quantile(outs,.05)
  outputs.uo[row.i,3] = quantile(outs,.95)
  }



outputs.sec = matrix(nrow = length(homo.scale.sec), ncol = 3)
for(i in homo.scale.sec){
  row.i = which(homo.scale.sec == i)
  outx = setx(reg.sec, outgroup.yeshiva = i, outgroup.dissim.yeshiva = mean.seg.value,
              jerusalem = 0,  	
              demo1.sex = 'male', 
              demo1.age = 40, 
              demo1.ethnicity = 'ash',
              demo2.left_right = 5.2,
              income = 'low', 
              high.school = 1, 
              nonjewish_pcnt = .06,
              immigrant = 0
              )
  
  sims = sim(reg.sec,outx, sims = n.sims)
  outs = sims$getqi(qi="ev")
  outputs.sec[row.i,1] = mean(outs)
  outputs.sec[row.i,2] = quantile(outs,.05)
  outputs.sec[row.i,3] = quantile(outs,.95)
  }


#homogeneity plots	
uo.frame = as.data.frame(cbind(outputs.uo,homo.scale.uo))
out.plot.uo.homo = ggplot(uo.frame, aes(homo.scale.uo,V1)) + 
  geom_smooth(color = 'black', aes(alpha = .75), se = FALSE) +
  geom_ribbon(aes(ymin = V2, ymax = V3), alpha = .05) +
  theme(legend.position="none") +
  labs(y = 'Bias in Sharing') + 
  scale_x_continuous(breaks = c(min(homo.scale.uo),max(homo.scale.uo)),
                     labels = c('Tel Aviv','Jerusalem')) +
  theme(
    plot.background = element_blank()
    #,panel.grid.major = element_blank()
    ,panel.grid.minor = element_blank()
    ,panel.border = element_blank(),
    axis.text=element_text(size=8),
    axis.title=element_text(size=12),
    plot.margin = unit(c(5,15,5,5), "pt"),
    axis.title.y=element_blank(),
    axis.text.y=element_text(color = 'white'),
    #   axis.ticks.y=element_blank(),
    axis.ticks.x=element_blank(),
    
    axis.title.x=element_blank(),
    axis.text.x=element_text(hjust=c(0,1), face = 'italic')
  ) + 
  coord_cartesian(ylim=c(0,10))

sec.frame = as.data.frame(cbind(outputs.sec,homo.scale.sec))
out.plot.sec.homo = ggplot(sec.frame, aes(homo.scale.sec,V1)) + 
  geom_smooth(color = 'black', aes(alpha = .75), se = FALSE) +
  geom_ribbon(aes(ymin = V2, ymax = V3), alpha = .05) +
  theme(legend.position="none") +
  labs(y = 'Bias in Sharing') + 
  scale_x_continuous(breaks = c(min(homo.scale.sec),max(homo.scale.sec)),
                     labels = c('Tel Aviv','Jerusalem')) +
  theme(
    plot.background = element_blank()
    ,panel.grid.minor = element_blank()
    ,panel.border = element_blank(),
    axis.text=element_text(size=8),
    axis.title=element_text(size=12),
    plot.margin = unit(c(5,15,5,5), "pt"),
    axis.title.y=element_blank(),
    axis.ticks.x=element_blank(),
    axis.title.x=element_blank(),
    axis.text.x=element_text(hjust=c(0,1), face = 'italic')
  ) + 
  coord_cartesian(ylim=c(0,10))



##combine plots
plot.combo.2 = cowplot::ggdraw(xlim = c(-.05,.95), ylim = c(-.05,1.05)) +
  cowplot::draw_plot(out.plot.sec.homo, x = 0, y = .525, width = .5, height = .5) +
  cowplot::draw_plot(out.plot.uo.homo, x = .45, y = .525, width = .5, height = .5) +
  cowplot::draw_plot(out.plot.sec.seg, x = 0, y = 0, width = .5, height = .5) +
  cowplot::draw_plot(out.plot.uo.seg, x = .45, y = 0, width = .5, height = .5) +
  cowplot::draw_text("Outgroup size",x = .5, y = 1.025, size = 11.5, vjust = .3) +
  cowplot::draw_text("Segregation",x = .5, y = .5,  size = 11.5, vjust = .3) +
  cowplot::draw_text("Group-based bias in sharing",x = -.025, y = .5, vjust = .5, angle = 90, size = 11.5) +
  cowplot::draw_text(c('Secular','ultra-Orthodox'), x = c(.26,.72), y = -.025,  size = 11.5)
cowplot::save_plot('Figure7_4.jpeg',
                   dpi = 1200,
                   plot.combo.2,
                   base_width = 4.66,
                   base_height = 7.33)

